using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace Microsoft.Phone.Controls
{
	[TemplatePart(Name = "Text", Type = typeof(TextBox)), TemplatePart(Name = "LengthIndicator", Type = typeof(TextBlock)), TemplatePart(Name = "HintContent", Type = typeof(ContentControl)), TemplateVisualState(Name = "Disabled", GroupName = "CommonStates"), TemplateVisualState(Name = "LengthIndicatorVisible", GroupName = "LengthIndicatorStates"), TemplateVisualState(Name = "ReadOnly", GroupName = "CommonStates"), TemplateVisualState(Name = "Unfocused", GroupName = "FocusStates"), TemplateVisualState(Name = "LengthIndicatorHidden", GroupName = "LengthIndicatorStates"), TemplateVisualState(Name = "Focused", GroupName = "FocusStates")]
	public class PhoneTextBox : TextBox
	{
		private const string RootGridName = "RootGrid";
		private const string TextBoxName = "Text";
		private const string HintContentName = "HintContent";
		private const string HintBorderName = "HintBorder";
		private const string LengthIndicatorName = "LengthIndicator";
		private const string ActionIconCanvasName = "ActionIconCanvas";
		private const string MeasurementTextBlockName = "MeasurementTextBlock";
		private const string ActionIconBorderName = "ActionIconBorder";
		private const string LengthIndicatorStates = "LengthIndicatorStates";
		private const string LengthIndicatorVisibleState = "LengthIndicatorVisible";
		private const string LengthIndicatorHiddenState = "LengthIndicatorHidden";
		private const string CommonStates = "CommonStates";
		private const string DisabledState = "Disabled";
		private const string ReadOnlyState = "ReadOnly";
		private const string FocusStates = "FocusStates";
		private const string FocusedState = "Focused";
		private const string UnfocusedState = "Unfocused";
		private Grid RootGrid;
		private TextBox TextBox;
		private TextBlock MeasurementTextBlock;
		private Brush ForegroundBrushInactive = (Brush)Application.get_Current().get_Resources().get_Item("PhoneTextBoxReadOnlyBrush");
		private Brush ForegroundBrushEdit;
		private ContentControl HintContent;
		private Border HintBorder;
		private TextBlock LengthIndicator;
		private Border ActionIconBorder;
		private bool _ignorePropertyChange;
		private bool _ignoreFocus;
		public static readonly DependencyProperty HintProperty = DependencyProperty.Register("Hint", typeof(string), typeof(PhoneTextBox), new PropertyMetadata(new PropertyChangedCallback(PhoneTextBox.OnHintPropertyChanged)));
		public static readonly DependencyProperty HintStyleProperty = DependencyProperty.Register("HintStyle", typeof(Style), typeof(PhoneTextBox), null);
		public static readonly DependencyProperty ActualHintVisibilityProperty = DependencyProperty.Register("ActualHintVisibility", typeof(Visibility), typeof(PhoneTextBox), null);
		public static readonly DependencyProperty LengthIndicatorVisibleProperty = DependencyProperty.Register("LengthIndicatorVisible", typeof(bool), typeof(PhoneTextBox), null);
		public static readonly DependencyProperty LengthIndicatorThresholdProperty = DependencyProperty.Register("LengthIndicatorThreshold", typeof(int), typeof(PhoneTextBox), new PropertyMetadata(new PropertyChangedCallback(PhoneTextBox.OnLengthIndicatorThresholdChanged)));
		public static readonly DependencyProperty DisplayedMaxLengthProperty = DependencyProperty.Register("DisplayedMaxLength", typeof(int), typeof(PhoneTextBox), new PropertyMetadata(new PropertyChangedCallback(PhoneTextBox.DisplayedMaxLengthChanged)));
		public static readonly DependencyProperty ActionIconProperty = DependencyProperty.Register("ActionIcon", typeof(ImageSource), typeof(PhoneTextBox), new PropertyMetadata(new PropertyChangedCallback(PhoneTextBox.OnActionIconChanged)));
		public static readonly DependencyProperty HidesActionItemWhenEmptyProperty = DependencyProperty.Register("HidesActionItemWhenEmpty", typeof(bool), typeof(PhoneTextBox), new PropertyMetadata(false, new PropertyChangedCallback(PhoneTextBox.OnActionIconChanged)));
		public event EventHandler ActionIconTapped;
		public string Hint
		{
			get
			{
				return base.GetValue(PhoneTextBox.HintProperty) as string;
			}
			set
			{
				base.SetValue(PhoneTextBox.HintProperty, value);
			}
		}
		public Style HintStyle
		{
			get
			{
				return base.GetValue(PhoneTextBox.HintStyleProperty) as Style;
			}
			set
			{
				base.SetValue(PhoneTextBox.HintStyleProperty, value);
			}
		}
		public Visibility ActualHintVisibility
		{
			get
			{
				return (Visibility)base.GetValue(PhoneTextBox.ActualHintVisibilityProperty);
			}
			set
			{
				base.SetValue(PhoneTextBox.ActualHintVisibilityProperty, value);
			}
		}
		public bool LengthIndicatorVisible
		{
			get
			{
				return (bool)base.GetValue(PhoneTextBox.LengthIndicatorVisibleProperty);
			}
			set
			{
				base.SetValue(PhoneTextBox.LengthIndicatorVisibleProperty, value);
			}
		}
		public int LengthIndicatorThreshold
		{
			get
			{
				return (int)base.GetValue(PhoneTextBox.LengthIndicatorThresholdProperty);
			}
			set
			{
				base.SetValue(PhoneTextBox.LengthIndicatorThresholdProperty, value);
			}
		}
		public int DisplayedMaxLength
		{
			get
			{
				return (int)base.GetValue(PhoneTextBox.DisplayedMaxLengthProperty);
			}
			set
			{
				base.SetValue(PhoneTextBox.DisplayedMaxLengthProperty, value);
			}
		}
		public ImageSource ActionIcon
		{
			get
			{
				return base.GetValue(PhoneTextBox.ActionIconProperty) as ImageSource;
			}
			set
			{
				base.SetValue(PhoneTextBox.ActionIconProperty, value);
			}
		}
		public bool HidesActionItemWhenEmpty
		{
			get
			{
				return (bool)base.GetValue(PhoneTextBox.HidesActionItemWhenEmptyProperty);
			}
			set
			{
				base.SetValue(PhoneTextBox.HidesActionItemWhenEmptyProperty, value);
			}
		}
		private static void OnHintPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
		{
			PhoneTextBox phoneTextBox = sender as PhoneTextBox;
			if (phoneTextBox != null && phoneTextBox.HintContent != null)
			{
				phoneTextBox.UpdateHintVisibility();
			}
		}
		private void UpdateHintVisibility()
		{
			if (this.HintContent != null)
			{
				if (string.IsNullOrEmpty(base.get_Text()))
				{
					this.ActualHintVisibility = 0;
					base.set_Foreground(this.ForegroundBrushInactive);
					return;
				}
				this.ActualHintVisibility = 1;
			}
		}
		protected override void OnLostFocus(RoutedEventArgs e)
		{
			this.UpdateHintVisibility();
			base.OnLostFocus(e);
		}
		protected override void OnGotFocus(RoutedEventArgs e)
		{
			if (this._ignoreFocus)
			{
				this._ignoreFocus = false;
				Frame frame = Application.get_Current().get_RootVisual() as Frame;
				frame.Focus();
				return;
			}
			base.set_Foreground(this.ForegroundBrushEdit);
			if (this.HintContent != null)
			{
				this.ActualHintVisibility = 1;
			}
			base.OnGotFocus(e);
		}
		private static void OnLengthIndicatorThresholdChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
		{
			PhoneTextBox phoneTextBox = sender as PhoneTextBox;
			if (phoneTextBox._ignorePropertyChange)
			{
				phoneTextBox._ignorePropertyChange = false;
				return;
			}
			if (phoneTextBox.LengthIndicatorThreshold < 0)
			{
				phoneTextBox._ignorePropertyChange = true;
				phoneTextBox.SetValue(PhoneTextBox.LengthIndicatorThresholdProperty, args.get_OldValue());
				throw new ArgumentOutOfRangeException("LengthIndicatorThreshold", "The length indicator visibility threshold must be greater than zero.");
			}
		}
		private static void DisplayedMaxLengthChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
		{
			PhoneTextBox phoneTextBox = sender as PhoneTextBox;
			if (phoneTextBox.DisplayedMaxLength > phoneTextBox.get_MaxLength() && phoneTextBox.get_MaxLength() > 0)
			{
				throw new ArgumentOutOfRangeException("DisplayedMaxLength", "The displayed maximum length cannot be greater than the MaxLength.");
			}
		}
		private void UpdateLengthIndicatorVisibility()
		{
			if (this.RootGrid != null && this.LengthIndicator != null)
			{
				bool flag = true;
				if (this.LengthIndicatorVisible)
				{
					this.LengthIndicator.set_Text(string.Format(CultureInfo.get_InvariantCulture(), "{0}/{1}", new object[]
					{
						base.get_Text().get_Length(),
						(this.DisplayedMaxLength > 0) ? this.DisplayedMaxLength : base.get_MaxLength()
					}));
					if (base.get_Text().get_Length() >= this.LengthIndicatorThreshold)
					{
						flag = false;
					}
				}
				VisualStateManager.GoToState(this, flag ? "LengthIndicatorHidden" : "LengthIndicatorVisible", false);
				return;
			}
		}
		private static void OnActionIconChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
		{
			PhoneTextBox phoneTextBox = sender as PhoneTextBox;
			if (phoneTextBox != null)
			{
				phoneTextBox.UpdateActionIconVisibility();
			}
		}
		private void UpdateActionIconVisibility()
		{
			if (this.ActionIconBorder != null)
			{
				if (this.ActionIcon == null || (this.HidesActionItemWhenEmpty && string.IsNullOrEmpty(base.get_Text())))
				{
					this.ActionIconBorder.set_Visibility(1);
					this.HintBorder.set_Padding(new Thickness(0.0));
					return;
				}
				this.ActionIconBorder.set_Visibility(0);
				if (base.get_TextWrapping() != 2)
				{
					this.HintBorder.set_Padding(new Thickness(0.0, 0.0, 48.0, 0.0));
				}
			}
		}
		private void OnActionIconTapped(object sender, RoutedEventArgs e)
		{
			this._ignoreFocus = true;
			EventHandler actionIconTapped = this.ActionIconTapped;
			if (actionIconTapped != null)
			{
				actionIconTapped.Invoke(sender, e);
			}
		}
		private void ResizeTextBox()
		{
			if (this.ActionIcon != null)
			{
				if (base.get_TextWrapping() == 2)
				{
					this.MeasurementTextBlock.set_Width(base.get_ActualWidth());
					if (this.MeasurementTextBlock.get_ActualHeight() > base.get_ActualHeight() - 72.0)
					{
						base.set_Height(base.get_ActualHeight() + 72.0);
						return;
					}
					if (base.get_ActualHeight() > this.MeasurementTextBlock.get_ActualHeight() + 144.0)
					{
						base.set_Height(base.get_ActualHeight() - 72.0);
					}
					return;
				}
			}
		}
		public PhoneTextBox()
		{
			base.set_DefaultStyleKey(typeof(PhoneTextBox));
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			if (this.TextBox != null)
			{
				this.TextBox.remove_TextChanged(new TextChangedEventHandler(this.OnTextChanged));
			}
			if (this.ActionIconBorder != null)
			{
				this.ActionIconBorder.remove_MouseLeftButtonDown(new MouseButtonEventHandler(this.OnActionIconTapped));
			}
			this.RootGrid = (base.GetTemplateChild("RootGrid") as Grid);
			this.TextBox = (base.GetTemplateChild("Text") as TextBox);
			this.ForegroundBrushEdit = base.get_Foreground();
			this.HintContent = (base.GetTemplateChild("HintContent") as ContentControl);
			this.HintBorder = (base.GetTemplateChild("HintBorder") as Border);
			if (this.HintContent != null)
			{
				this.UpdateHintVisibility();
			}
			this.LengthIndicator = (base.GetTemplateChild("LengthIndicator") as TextBlock);
			this.ActionIconBorder = (base.GetTemplateChild("ActionIconBorder") as Border);
			if (this.RootGrid != null && this.LengthIndicator != null)
			{
				this.UpdateLengthIndicatorVisibility();
			}
			if (this.TextBox != null)
			{
				this.TextBox.add_TextChanged(new TextChangedEventHandler(this.OnTextChanged));
			}
			if (this.ActionIconBorder != null)
			{
				this.ActionIconBorder.add_MouseLeftButtonDown(new MouseButtonEventHandler(this.OnActionIconTapped));
				this.UpdateActionIconVisibility();
			}
			this.MeasurementTextBlock = (base.GetTemplateChild("MeasurementTextBlock") as TextBlock);
		}
		private void OnTextChanged(object sender, RoutedEventArgs e)
		{
			this.UpdateLengthIndicatorVisibility();
			this.UpdateActionIconVisibility();
			this.UpdateHintVisibility();
			this.ResizeTextBox();
		}
	}
}
